//
//  LDSTableViewModel.m
//  LDSMVVMBase
//
//  Created by kai_leedarson on 2017/9/30.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSTableViewModel.h"
#import "LDSTableViewCellViewModel.h"
#import "LDSViewModel.h"

@interface LDSTableViewModel ()
@property (nonatomic, strong) NSMutableArray *viewModels;
@property (nonatomic, strong) NSMutableArray *sectionViewModels;
@end

@implementation LDSTableViewModel
#pragma mark - lazy init
- (NSMutableArray *)sectionViewModels {
    if (_sectionViewModels == nil) {
        _sectionViewModels = [NSMutableArray arrayWithCapacity:0];
    }
    return _sectionViewModels;
}

- (NSMutableArray *)viewModels {
    if (_viewModels == nil) {
        _viewModels = [NSMutableArray array];
    }
    return _viewModels;
}

- (void)handleWithTable:(UITableView *)tableView {
    if (tableView == nil) {
        NSLog(@"无tableview");
        return;
    }
//    tableView.contentInsetAdjustmentBehavior = YES;
    tableView.delegate = (id <UITableViewDelegate> )self;
    tableView.dataSource = (id <UITableViewDataSource>)self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    
}

- (void)handleWithDataSource:(NSMutableArray *)dataSource {
    self.viewModels = dataSource ;
}

- (void)handleWithSectionDataSoure:(NSMutableArray *)sectionDataSource {
    self.sectionViewModels = sectionDataSource;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionViewModels.count >0 ? self.sectionViewModels.count : self.viewModels.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModels.count > section ? [self.viewModels[section] count]:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LDSTableViewCellViewModel *vModel = [[self.viewModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    vModel.tableView = tableView;

    LDSBaseTableViewCell *cell = nil;
    if (cell == nil) {
        cell = (LDSBaseTableViewCell *) [vModel cell];
    }
    [cell setModel:vModel.model];
    if ([self hasOtherDelegate ]) {
        [self cellAddDelegate:cell];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LDSTableViewCellViewModel *vModel = [[self.viewModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return [vModel height];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.sectionViewModels.count >0) {
        LDSViewModel *vModel = [self.sectionViewModels objectAtIndex:section];
        if (vModel && [vModel isKindOfClass:[LDSViewModel class]]) {
            return vModel.view.frame.size.height;
        }
    }
    return section == 0 ? [self firstSectionHeight]: [self otherSectionHeght];
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor clearColor];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(lds_tableView:didSelectRowAtIndexPath:)]) {
        [self.delegate lds_tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.sectionViewModels.count >0) {
        LDSViewModel *vModel = [self.sectionViewModels objectAtIndex:section];
        if (vModel && [vModel isKindOfClass:[LDSViewModel class]]) {
            LDSBaseView *LDSBaseView = vModel.view;
            LDSBaseView.delegate = (id<LDSBaseViewDelegate>) self;
            [LDSBaseView setModel:vModel.model];
            LDSBaseView.tag = vModel.tag;
            return vModel.view;
        }
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == 0 ? [self firstSectionFootHeight]: [self otherSectionfootHeight];
}

#pragma mark - LDSBaseViewDelegate
- (BOOL)view:(UIView *)view index:(NSUInteger)index model:(id)model {
    return YES;
}

#pragma mark - privite
- (void)cellAddDelegate:(id )cell {
    
}

- (BOOL)hasOtherDelegate {
    return NO;
}

- (CGFloat )firstSectionHeight {
    return self.firstSectionHeadViewHeight > 0 ? self.firstSectionHeadViewHeight : 27.0f;
}

- (CGFloat )otherSectionHeght {
    return self.otherSectionHeadViewHeight > 0 ? self.otherSectionHeadViewHeight : 35.0f;
}

- (CGFloat )firstSectionFootHeight {
    return self.firstSectionFootViewHeight > 0 ? self.firstSectionFootViewHeight: 0.01f;
}

- (CGFloat )otherSectionfootHeight {
    return self.otherSectionFootViewHeight > 0 ? self.otherSectionFootViewHeight: 0.01f;
}




@end
